package com.appspot.yttv.client;

import java.util.List;

import com.appspot.yttv.client.model.Station;
import com.appspot.yttv.client.model.VideoCategory;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventPreview;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * The main application
 *  
 * @author Uwe Maurer
 */

public class YouTubeTv implements EntryPoint {
	private final TitleLabel title = new TitleLabel("title", "");
	private final StationLabel station = new StationLabel();
	private final ChromelessPlayer player = new ChromelessPlayer("leTube", 
			Window.getClientWidth(), Window.getClientHeight());
	private final StaticPanel staticPanel = new StaticPanel(
			Window.getClientWidth(), Window.getClientHeight());
	private final Presenter presenter = new Presenter(player, title, station, staticPanel);
    private Label hint = new Label();
    
	public void onModuleLoad() {
		Panel p = new FocusPanel();
		RootPanel.get().add(Console.init());

		p.add(player);
	    p.setStyleName("player");
		RootPanel.get().add(p);
		
		RootPanel.get().add(title.getPanel());
		RootPanel.get().add(staticPanel);
		RootPanel.get().add(station.getLabel());
		
        hint.setText("(P)rev (N)ext (S)kip");
        hint.setStyleName("hint");
        RootPanel.get().add(hint);
		
		DeferredCommand.addCommand(new Command() {
			public void execute() {
				VideoCategoryService service = new VideoCategoryService();
				List<VideoCategory> categories = service.findCategories();
				for (VideoCategory category: categories) {
					presenter.addStation(new Station(category.getLabel(),
							new FeedPlaylistProvider(FeedUrlProvider.forVideoCategory(category))));
				}
				presenter.play();
				
				StationConfigurationService configService = new StationConfigurationService();
				configService.findStations( new AsyncCallback<List<Station>>() {
					public void onFailure(Throwable caught) {
						// Do nothing.
					}
					public void onSuccess(List<Station> result) {
						presenter.addAllStations(result);
					}
				});
			}
		});
		
	    class KeyController implements EventPreview {
			public boolean onEventPreview(Event event) {
				if ((event.getTypeInt() & Event.ONKEYPRESS) == 0) {
					return true;  // Allow for other processing.
				}
				int key = event.getKeyCode();
				switch (key) {
				case 'P':
				case 'p':
					presenter.previousStation();
					return false;
				case 'N':
				case 'n':
					presenter.nextStation();
					return false;
				case 'S':
				case 's':
					presenter.skipVideo();
					return false;
				case 'D':
				case 'd':
					Console.toggle();
					return false;
				case 'H':
				case 'h':
					presenter.toggleVideoQuality();
					return false;
				}
				return true;
			}
		}
		DOM.addEventPreview(new KeyController());
		
		// Resize the player and static panel on window resize.
		WindowResizeListener resizeListener = new WindowResizeListener() {
			public void onWindowResized(int width, int height) {
				player.setPixelSize(width, height);
				staticPanel.setPixelSize(width, height);
			}
	    };
	    Window.addWindowResizeListener(resizeListener);
	}
}
